package com.java.main.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.java.main.constant.Errors;
import com.java.main.controller.input.FileInput;
import com.java.main.controller.input.StringInput;
import com.java.main.controller.output.BaseOutput;
import com.java.main.controller.output.ResultResponseBeanUtil;
import com.java.main.exception.HiBabyGlobalException;
import com.java.main.util.AliHttpUtils;
import com.java.main.util.CnToSpell;
//import com.hibaby.crm.util.JedisUtil;
import com.java.main.util.JsonUtil;
import com.java.main.util.OSSUploadUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(value = "通用工具类", description = "通用工具类 by 田春阳", produces = "application/json")
@Controller
public class ToolsController extends BaseController {

	@Value("${ali_appCode}")
	private String aliAppCode;

	@ApiOperation(value = "汉字转拼音首字符", notes = "汉字转拼音首字符")
	@ResponseBody
	@RequestMapping(value = "/chineseToPinyin", method = RequestMethod.POST)
	public BaseOutput<String> chineseToPinyin(@RequestBody PinyinInput input) {
		String str = input.getStr();
		if (str == null) {
			return ResultResponseBeanUtil.fail(Errors.FORM_VALID);
		}
		return ResultResponseBeanUtil.success(CnToSpell.getPinYinHeadChar(str));
	}

	@ApiOperation(value = "上传单个附件", notes = "上传单个附件")
	@ResponseBody
	@RequestMapping(value = "/uploadEnclosure", method = RequestMethod.POST)
	public BaseOutput<OSSFileObject> uploadEnclosure(@RequestParam(value = "file") MultipartFile multipartFile) {
		OSSFileObject oss = null;
		if (multipartFile != null && multipartFile.getSize() > 0) {
			String name = multipartFile.getOriginalFilename();
			String filetype = name.substring(name.indexOf(".") + 1, name.length());
			oss = OSSUploadUtil.uploadMultipartFile(multipartFile, filetype);
		} else {
			throw new HiBabyGlobalException(Errors.LACK_CONTENT);
		}
		return ResultResponseBeanUtil.success(oss);
	}

	@ApiOperation(value = "上传头像", notes = "上传头像,返回filekey和头像url")
	@ResponseBody
	@RequestMapping(value = "/uploadImg", method = RequestMethod.POST)
	public BaseOutput<OSSFileObject> uploadImg(@RequestParam(value = "file") MultipartFile multipartFile) {
		OSSFileObject oss = null;
		if (multipartFile != null && multipartFile.getSize() > 0) {
			String name = multipartFile.getOriginalFilename();
			String filetype = name.substring(name.indexOf(".") + 1, name.length());
			oss = OSSUploadUtil.uploadMultipartFile(multipartFile, filetype);
			String url = OSSUploadUtil.getUrl(oss.getFileKey());
			List<String> list = new ArrayList<>();
			list.add(url);
			oss.setFileUrlList(list);
		} else {
			throw new HiBabyGlobalException(Errors.LACK_CONTENT);
		}
		return ResultResponseBeanUtil.success(oss);
	}

	@ApiOperation(value = "根据filekey获取附件下载URL", notes = "根据filekey获取附件下载URL")
	@ResponseBody
	@RequestMapping(value = "/getFileURL", method = RequestMethod.POST)
	public BaseOutput<OSSFileObject> getFileURL(HttpServletRequest request, HttpServletResponse response,
			@Valid @RequestBody FileInput input) {
		OSSFileObject oss = new OSSFileObject();
		String[] keys = input.getFileKey().split("\\|");
		String url = "";
		List<String> list = new ArrayList<>();
		for (int i = 0; i < keys.length; i++) {
			url = OSSUploadUtil.getUrl(keys[i]);
			list.add(url);
		}
		oss.setFileUrlList(list);
		return ResultResponseBeanUtil.success(oss);
	}

//	@ApiOperation(value = "获取缓存信息", notes = "获取缓存信息")
//	@ResponseBody
//	@RequestMapping(value = "/getRedisInfo", method = RequestMethod.POST)
//	public BaseOutput<String> getFileURL(@RequestBody NameInput input) {
//		Object object = JedisUtil.getObj(input.getName());
//		String json = "";
//		try {
//			json = JsonUtil.beanToJson(object);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		return ResultResponseBeanUtil.success(json);
//	}

	@ApiOperation(value = "获取服务器时间", notes = "获取服务器时间")
	@ResponseBody
	@RequestMapping(value = "/getSystemTime", method = RequestMethod.POST)
	public BaseOutput<Long> getSystemTime() {
		Long time = System.currentTimeMillis();
		return ResultResponseBeanUtil.success(time);
	}

	@ApiOperation(value = "查询手机号码归属地", notes = "查询手机号码归属地")
	@ResponseBody
	@RequestMapping(value = "/getNumberAscription", method = RequestMethod.POST)
	public BaseOutput<NumberAscriptionOutput> getNumberAscription(@RequestBody StringInput input) {
		String host = "http://jshmgsdmfb.market.alicloudapi.com";
		String path = "/shouji/query";
		String method = "GET";
		Map<String, String> headers = new HashMap<String, String>();
		headers.put("Authorization", "APPCODE " + aliAppCode);
		Map<String, String> querys = new HashMap<String, String>();
		querys.put("shouji", input.getStr());

		NumberAscriptionOutput result = new NumberAscriptionOutput();
		try {
			HttpResponse response = AliHttpUtils.doGet(host, path, method, headers, querys);
			String aliJson = EntityUtils.toString(response.getEntity());
			Map<String, Object> aliMap = JsonUtil.jsonToMap(aliJson);
			String msg = (String) aliMap.get("msg");
			if ("ok".equals(msg)) {
				Map<String, Object> resultMap = (Map<String, Object>) aliMap.get("result");
				result.setCardtype(resultMap.get("cardtype") + "");
				result.setCity(resultMap.get("city") + "");
				result.setCompany(resultMap.get("company") + "");
				result.setProvince(resultMap.get("province") + "");
			} else {
				return ResultResponseBeanUtil.fail(Errors.SYSTEM_ERROR, msg);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResultResponseBeanUtil.success(result);
	}
}
